<template>
  <!-- 宠物用户页顶部条 -->
  <div style="height:6vh;padding: 10px 20px;background-color:#fff;">
    <span style="line-height: 60px;font-size:20px;">宠物医疗管理</span>
    <el-button type="primary" style="float:right;margin-top: 10px; margin-right: 10px;"
               @click="beforeAddMedicalRecord">新建病历</el-button>
  </div>

  <!-- 宠物病历弹窗 -->
  <el-dialog
      :title="medicalRecordDialogTitle"
      style="width: 1000px;padding: 40px;"
      v-model="medicalRecordDialogVisible"
      :before-close="handleMedicalRecordClose"
  >
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="宠物名称">
            <el-input placeholder="请输入宠物名称" v-model="saveMedicalRecordForm.petName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="宠物类型">
            <el-input placeholder="请输入宠物类型" v-model="saveMedicalRecordForm.petType"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="症状描述">
            <el-input placeholder="请输入症状描述" v-model="saveMedicalRecordForm.symptom"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="诊断结果">
            <el-input placeholder="请输入诊断结果" v-model="saveMedicalRecordForm.diagnosis"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="治疗方案">
            <el-input placeholder="请输入治疗方案" v-model="saveMedicalRecordForm.treatment"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="兽医姓名">
            <el-input placeholder="请输入兽医姓名" v-model="saveMedicalRecordForm.veterinarianName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleMedicalRecordClose">取消</el-button>
      <el-button type="primary" @click="saveMedicalRecord">保存</el-button>
    </template>
  </el-dialog>





  <!-- 宠物病历列表表格 -->
  <el-card style="margin: 20px;">
    <el-table :data="medicalRecordArr">
      <el-table-column type="index" label="编号" width="80" align="center"></el-table-column>
      <el-table-column label="宠物名称" prop="petName" align="center"></el-table-column>
      <el-table-column label="宠物类型" prop="petType" align="center"></el-table-column>
      <el-table-column label="症状描述" prop="symptom" align="center"></el-table-column>
      <el-table-column label="诊断结果" prop="diagnosis" align="center"></el-table-column>
      <el-table-column label="治疗方案" prop="treatment" align="center"></el-table-column>
      <el-table-column label="兽医姓名" prop="veterinarianName" align="center"></el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editMedicalRecord(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteMedicalRecord(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";

// 定义变量用来控制弹窗是否可见
const dialogVisible = ref(false);
// 宠物病历相关
// 定义变量用来控制病历弹窗是否可见
const medicalRecordDialogVisible = ref(false);
// 定义对象用来收集宠物病历表单数据
const saveMedicalRecordForm = ref({
  petName: '',
  petType: '',
  symptom: '',
  diagnosis: '',
  treatment: '',
  veterinarianName: ''
});
// 定义变量用来保存病历弹窗的标题
const medicalRecordDialogTitle = ref('新建病历');
// 定义点击"新建病历"按钮时调用的方法
const beforeAddMedicalRecord = () => {
  medicalRecordDialogTitle.value = '新建病历';
  medicalRecordDialogVisible.value = true;
}
// 定义保存宠物病历的方法
const saveMedicalRecord = () => {
  let data = qs.stringify(saveMedicalRecordForm.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/medicalRecord/save', data).then((response) => {
    if (response.data.code === 2000) {
      ElMessage.success('病历保存成功!');
      saveMedicalRecordForm.value = {};
      medicalRecordDialogVisible.value = false;
      loadMedicalRecord();
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 定义处理病历弹窗关闭的方法
const handleMedicalRecordClose = () => {
  if (confirm('您确认要关闭病历弹窗吗?')) {
    saveMedicalRecordForm.value = {};
    medicalRecordDialogVisible.value = false;
  }
}
// 定义数组用来保存宠物病历列表表格数据
const medicalRecordArr = ref([]);
// 定义加载宠物病历列表的方法
const loadMedicalRecord = () => {
  axios.get(BASE_URL + '/v1/medicalRecord/select').then((response) => {
    if (response.data.code === 2000) {
      medicalRecordArr.value = response.data.data;
      console.log(medicalRecordArr.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
// 定义编辑宠物病历的方法
const editMedicalRecord = (id) => {
  medicalRecordDialogVisible.value = true;
  medicalRecordDialogTitle.value = '编辑病历';
  axios.get(BASE_URL + '/v1/medicalRecord/select?id=' + id).then((response) => {
    if (response.data.code === 2000) {
      saveMedicalRecordForm.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg);
    }
  });
};

// 定义删除宠物病历的方法
const deleteMedicalRecord = (id) => {
  if (confirm('您确认要删除当前宠物病历吗?')) {
    axios.post(BASE_URL + '/v1/medicalRecord/delete/' + id).then((response) => {
      if (response.data.code === 2000) {
        ElMessage.success('宠物病历删除成功!');
        loadMedicalRecord();
      } else {
        ElMessage.error(response.data.msg);
      }
    });
  }
};
</script>

<style scoped>

</style>